<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils.js";

export default {
  data() {
    return {
      allData: null, //存储请求过来的数据
      echartInstance: null, //服务器获取过来的数据
      fontSize: 0,
      currentIndex: 0,
      tiemId: null
    };
  },
  created() {
    this.$socket.registerCallBack("stockData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "stockData",
      chartName: "stock",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    // 在刚进入页面时，主动进行屏幕适配
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    this.$socket.unRegisterCallBack("stockData");
  },
  methods: {
    initChart() {
      this.echartInstance = this.$echarts.init(
        this.$refs.stock_ref,
        this.theme
      );
      this.echartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartInstance.on("mouseout", () => {
        this.startInterval();
      });
      const initOption = {
        title: {
          text: "▎库存与销量",
          top: 20,
          left: 20
        }
      };
      this.echartInstance.setOption(initOption);
    },
    getData(ret) {
      // const { data: ret } = await this.$http.get("stock");
      this.allData = ret;
      console.log(this.allData);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"]
      ];
      const colorArr = [
        ["#4ff778", "#0ba82c"],
        ["#e5dd45", "#e8b11c"],
        ["#e8821c", "#e55445"],
        ["#5052ee", "#ab6ee5"],
        ["#23e5e5", "#2e72bf"]
      ];
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      const showData = this.allData.slice(start, end);
      const seriesArr = showData.map((item, i) => {
        return {
          type: "pie",
          radius: [this.fontSize * 2.8125, this.fontSize * 2.5],
          center: centerArr[i],
          hoverAnimation: false,
          labelLine: {
            show: false
          },
          label: {
            position: "center",
            fontSize: this.fontSize / 1.5,
            color: this.labelColor
          },

          data: [
            {
              name: item.name + "\n\n" + item.sales,
              value: item.sales,
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: colorArr[i][0] },
                  { offset: 1, color: colorArr[i][1] }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: "#333843"
              }
            }
          ]
        };
      });
      const dataOption = {
        series: seriesArr
      };
      this.echartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.fontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      // 分辨率相关配置相关
      const adapterOption = {
        title: {
          // 设置标题样式
          textStyle: {
            fontSize: this.fontSize
          }
        }
      };
      this.echartInstance.setOption(adapterOption);
      this.echartInstance.resize();
    },
    startInterval() {
      const maxPage = Math.ceil(this.allData.length / 5);
      console.log(maxPage);
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > maxPage - 1) {
          this.currentIndex = 0;
        }
        this.updateChart();
      }, 2000);
    }
  },
  computed: {
    ...mapState(["theme"]),
    labelColor() {
      return getThemeValue(this.theme).titleColor;
    }
  },
  watch: {
    theme() {
      // 销毁当前图表
      this.echartInstance.dispose();
      // 重新创建图表
      this.initChart();
      // 完成屏幕适配
      this.screenAdapter();
      // 更新图表的展示
      this.updateChart();
    }
  }
};
</script>
<style scoped lang=''>
</style>